package client.gui;

import java.awt.Button;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileNotFoundException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import shared.player.Player;
import shared.util.Config;
import client.game.GameWindow;
import client.lobby.WaitingLobby;

/**
 * The Class ContinueGui
 * - gui for asking if the client wishes to play again.
 */
public class ContinueGui extends JPanel {

	/** The universal version identifier. */
	private static final long serialVersionUID = -2688721225109576841L;
	
	/** The JFrame on which the continueGui panel is added. */
	JFrame frame;
	
	/** The winner of the game. */
	Player winner;
	
	/** Traffic out. */
	PrintWriter netOut;
	
	/** The gameWindow on which the game was played. */
	GameWindow gameWindow;
	
	/** The name of the player. */
	String name;
	
	/** Traffic in. */
	ObjectInputStream netIn;

	/**
	 * Instantiates a new continue gui.
	 *
	 * @param winner the winner of the game
	 * @param netIn the traffic in
	 * @param netOut the traffic out
	 * @param gameWindow The gameWindow on which the game was played.
	 * @param name the name of the player
	 */
	public ContinueGui(Player winner, ObjectInputStream netIn, PrintWriter netOut, GameWindow gameWindow, String name) {
		this.name = name;
		this.winner = winner;
		this.netOut = netOut;
		this.netIn = netIn;
		this.gameWindow = gameWindow;
		run();
	}

	/**
	 * Run
	 * - creates the gui.
	 */
	public void run() {
		frame = new JFrame("Continue playing?");
		frame.setSize(290, 100);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		this.setVisible(true);
		JLabel lbl = new JLabel("", JLabel.CENTER);
		JLabel lbl2 = new JLabel("Play more?", JLabel.CENTER);
		if (winner != null) {
			lbl.setText("Winner: " + winner.name);
		}else{
			lbl.setText("Tie game!");
		}
		lbl.setVisible(true);
		this.setLayout(new GridLayout(2, 2));
		
		Button yes = new Button("Yes");
		Button no = new Button("No");
		frame.add(this);
		this.add(lbl);
		this.add(lbl2);

		yes.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				disposeAll();
				try {
					new WaitingLobby(name, netIn, netOut, new Config("config.xml"));
				} catch (FileNotFoundException e1) {
					new ErrorMessageGui("Config file not found.");
				}
				netOut.println("REPLAY");
			}
		});
		no.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				disposeAll();
				netOut.println("DC");
			}
		});

		this.add(yes);
		this.add(no);
	}

	/**
	 * Dispose
	 * - disposes of the continue gui JFrame.
	 */
	public void dispose() {
		frame.dispose();
	}
	
	/**
	 * Dispose all
	 * - disposes on continue gui JFrame and gameWindow.
	 */
	private void disposeAll(){
		gameWindow.dispose();
		this.dispose();
	}
}
